package cn.iocoder.yudao.module.cockpit.controller.admin.areatotal;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.cockpit.controller.admin.areatotal.vo.AreaTotalPageReqVO;
import cn.iocoder.yudao.module.cockpit.controller.admin.areatotal.vo.AreaTotalRespVO;
import cn.iocoder.yudao.module.cockpit.controller.admin.areatotal.vo.AreaTotalSaveReqVO;
import cn.iocoder.yudao.module.cockpit.dal.dataobject.areatotal.AreaTotalDO;
import cn.iocoder.yudao.module.cockpit.service.areatotal.AreaTotalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 区域统计")
@RestController
@RequestMapping("/cockpit/area-total")
@Validated
public class AreaTotalController {

    @Resource
    private AreaTotalService areaTotalService;

    @PostMapping("/create")
    @Operation(summary = "创建区域统计")
    @PreAuthorize("@ss.hasPermission('cockpit:area-total:create')")
    public CommonResult<Long> createAreaTotal(@Valid @RequestBody AreaTotalSaveReqVO createReqVO) {
        return success(areaTotalService.createAreaTotal(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新区域统计")
    @PreAuthorize("@ss.hasPermission('cockpit:area-total:update')")
    public CommonResult<Boolean> updateAreaTotal(@Valid @RequestBody AreaTotalSaveReqVO updateReqVO) {
        areaTotalService.updateAreaTotal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除区域统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cockpit:area-total:delete')")
    public CommonResult<Boolean> deleteAreaTotal(@RequestParam("id") Long id) {
        areaTotalService.deleteAreaTotal(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得区域统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cockpit:area-total:query')")
    public CommonResult<AreaTotalRespVO> getAreaTotal(@RequestParam("id") Long id) {
        AreaTotalDO areaTotal = areaTotalService.getAreaTotal(id);
        return success(BeanUtils.toBean(areaTotal, AreaTotalRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得区域统计分页")
    @PreAuthorize("@ss.hasPermission('cockpit:area-total:query')")
    public CommonResult<PageResult<AreaTotalRespVO>> getAreaTotalPage(@Valid AreaTotalPageReqVO pageReqVO) {
        PageResult<AreaTotalDO> pageResult = areaTotalService.getAreaTotalPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AreaTotalRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得list")
    @PermitAll
    public CommonResult<List<AreaTotalRespVO>> getAreaTotalList(
            @Valid AreaTotalPageReqVO pageReqVO) {
        List<AreaTotalDO> pageResult = areaTotalService.getList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AreaTotalRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出区域统计 Excel")
    @PreAuthorize("@ss.hasPermission('cockpit:area-total:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAreaTotalExcel(@Valid AreaTotalPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AreaTotalDO> list = areaTotalService.getAreaTotalPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "区域统计.xls", "数据", AreaTotalRespVO.class,
                        BeanUtils.toBean(list, AreaTotalRespVO.class));
    }

}